﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Electric;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YArchitech.Revit.Electric.Common;
using YArchitech.Revit.MEP.Electric.Calculator;
using HYElectricCalculation;
using HYElectricCommon;

namespace YArchitech.Revit.MEP.Calculator
{
	public partial class CaculateCount : System.Windows.Forms.Form
	{
		public CaculateCount(ExternalEvent exEvent, RequestHandler handler, UIApplication ui_app)
		{
			this.InitializeComponent();
			this.ui_app = ui_app;
			this.doc = ui_app.ActiveUIDocument.Document;
			this.UIrvt_doc = ui_app.ActiveUIDocument;
			CaculateCount.activeView = this.UIrvt_doc.ActiveView.Document.ActiveView;
			this.m_Handler = handler;
			this.m_ExEvent = exEvent;
		}

		public CaculateCount(UIApplication ui_app)
		{
			this.InitializeComponent();
			this.ui_app = ui_app;
			this.doc = ui_app.ActiveUIDocument.Document;
			this.UIrvt_doc = ui_app.ActiveUIDocument;
			CaculateCount.activeView = this.UIrvt_doc.ActiveView.Document.ActiveView;
		}

		private void CaculateCount_Load(object sender, EventArgs e)
		{
			CaculateCount.i = 0;
			this.dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None;
			//base.Icon = Resource.YArchitechIcon;
			if (CaculateCount.activeView.GenLevel != null)
			{
				if (!CaculateCount.tabLevels.TabPages.ContainsKey(CaculateCount.activeView.GenLevel.Name))
				{
					TabPage tabPage = new TabPage();
					tabPage.Name = CaculateCount.activeView.GenLevel.Name;
					tabPage.Text = CaculateCount.activeView.GenLevel.Name;
					tabPage.Controls.Add(this.dataGridView1);
					this.dataGridView1.Dock = DockStyle.Fill;
					CaculateCount.tabLevels.TabPages.Add(tabPage);
					return;
				}
			}
			else
			{
				MessageBox.Show("请在平面图下进行校核");
			}
		}

		public static void AddRoomData(IllCalDataSet illCalDataSet, bool isAdd)
		{
			if (isAdd)
			{
				CaculateCount.liCalculate.Clear();
			}
			for (int i = 0; i < illCalDataSet.listIllCalData.Count; i++)
			{
				CmdCaculaterDate cmdCaculaterDate = new CmdCaculaterDate();
				IllCalData illCalData = illCalDataSet.listIllCalData[i];
				List<string> calculateErrorInformation = illCalData.CalculateErrorInformation;
				string text = null;
				foreach (string str in calculateErrorInformation)
				{
					text += str;
				}
				cmdCaculaterDate.ErrorLog = text;
				if (!CaculateCount.roomNumList.Contains(illCalData.roomData.RoomNumber))
				{
					cmdCaculaterDate.Num = CaculateCount.j;
					if (illCalData.roomData.RoomNumber.Trim() != "")
					{
						cmdCaculaterDate.RoomNum = illCalData.roomData.RoomNumber;
					}
					else
					{
						cmdCaculaterDate.RoomNum = "0";
					}
					cmdCaculaterDate.RoomName = illCalData.roomData.RoomName;
					cmdCaculaterDate.StantardIllumi = illCalData.roomData.Lux.ToString();
					cmdCaculaterDate.ActualIllumi = illCalData.calResultData.Lux.ToString("0.00");
					cmdCaculaterDate.StantardPowerDensity = illCalData.roomData.Lpd.ToString();
					cmdCaculaterDate.ActualPowerDensity = illCalData.calResultData.Lpd.ToString("0.00");
					cmdCaculaterDate.ActualDistanceRadio = illCalData.roomData.DinstinceHight.ToString();
					cmdCaculaterDate.MaxDistanceRadio = illCalData.roomData.DinstinceHight.ToString();
					cmdCaculaterDate.LuxCaculate = illCalData.calResultData.LuxChecked;
					cmdCaculaterDate.LpdCaculate = illCalData.calResultData.LpdChecked;
					CaculateCount.roomNumList.Add(illCalData.roomData.RoomNumber);
					CaculateCount.liCalculate.Add(cmdCaculaterDate);
					CaculateCount.j++;
				}
			}
		}

		public static void AddGridDataSource(DataGridView dgv)
		{
			dgv.Rows.Clear();
			for (int i = 0; i < CaculateCount.liCalculate.Count; i++)
			{
				CmdCaculaterDate cmdCaculaterDate = CaculateCount.liCalculate[i];
				DataGridViewRow dataGridViewRow = new DataGridViewRow();
				int index = dgv.Rows.Add(dataGridViewRow);
				if (cmdCaculaterDate.LpdCaculate == CheckedState.eMeet && cmdCaculaterDate.LuxCaculate == CheckedState.eMeet)
				{
					dgv.Rows[index].Cells[0].Value = YArchitech.Electric.Resources.GetImage("SuccessFace16");
				}
				else if (cmdCaculaterDate.LpdCaculate == CheckedState.eUnknown || cmdCaculaterDate.LuxCaculate == CheckedState.eUnknown)
				{
					dgv.Rows[index].Cells[0].Value = YArchitech.Electric.Resources.GetImage("WaitingFace16");
				}
				else
				{
					dgv.Rows[index].Cells[0].Value = YArchitech.Electric.Resources.GetImage("FailedFace16");
				}
				dgv.Rows[index].Cells[0].Tag = cmdCaculaterDate.ErrorLog;
				dgv.Rows[index].Cells[1].Value = cmdCaculaterDate.Num.ToString();
				dgv.Rows[index].Cells[2].Value = cmdCaculaterDate.RoomNum.ToString();
				dgv.Rows[index].Cells[3].Value = cmdCaculaterDate.RoomName.ToString();
				if (Convert.ToDouble(cmdCaculaterDate.StantardIllumi) > Convert.ToDouble(cmdCaculaterDate.ActualIllumi))
				{
					dgv.Rows[index].Cells[5].Style.BackColor = System.Drawing.Color.Red;
				}
				dgv.Rows[index].Cells[4].Value = cmdCaculaterDate.StantardIllumi.ToString();
				dgv.Rows[index].Cells[5].Value = cmdCaculaterDate.ActualIllumi.ToString();
				if (Convert.ToDouble(cmdCaculaterDate.StantardPowerDensity) < Convert.ToDouble(cmdCaculaterDate.ActualPowerDensity))
				{
					dgv.Rows[index].Cells[7].Style.BackColor = System.Drawing.Color.Red;
				}
				dgv.Rows[index].Cells[6].Value = cmdCaculaterDate.StantardPowerDensity.ToString();
				dgv.Rows[index].Cells[7].Value = cmdCaculaterDate.ActualPowerDensity.ToString();
				if (cmdCaculaterDate.MaxDistanceRadio.ToString() == "不满足")
				{
					dgv.Rows[index].Cells[8].Style.BackColor = System.Drawing.Color.Red;
				}
				dgv.Rows[index].Cells[8].Value = cmdCaculaterDate.MaxDistanceRadio.ToString();
			}
		}

		private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
		{
		}

		private void btncheckAndContract_Click(object sender, EventArgs e)
		{
			if (CaculateCount.i % 2 == 0)
			{
				base.Size = new Size(779, 58);
				CaculateCount.i++;
				this.isDisplay = false;
				return;
			}
			base.Size = new Size(779, 459);
			CaculateCount.i++;
			this.isDisplay = true;
		}

		private void btnExsit_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void toolStripButton1_Click(object sender, EventArgs e)
		{
		}

		public static DataGridView CreatDataGridView()
		{
			DataGridView dataGridView = new DataGridView();
			dataGridView.CellDoubleClick += CaculateCount.dgv_CellDoubleClick;
			dataGridView.BackgroundColor = System.Drawing.Color.White;
			dataGridView.RowHeadersVisible = false;
			dataGridView.AllowUserToAddRows = false;
			dataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None;
			DataGridViewImageColumn dataGridViewImageColumn = new DataGridViewImageColumn();
			dataGridViewImageColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
			dataGridViewImageColumn.Width = 26;
			dataGridView.Columns.Add(dataGridViewImageColumn);
			DataGridViewTextBoxColumn dataGridViewTextBoxColumn = new DataGridViewTextBoxColumn();
			dataGridViewTextBoxColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
			dataGridViewTextBoxColumn.SortMode = DataGridViewColumnSortMode.NotSortable;
			dataGridViewTextBoxColumn.Width = 40;
			dataGridViewTextBoxColumn.Name = "序号";
			dataGridViewTextBoxColumn.HeaderText = "序号";
			dataGridView.Columns.Add(dataGridViewTextBoxColumn);
			DataGridViewTextBoxColumn dataGridViewTextBoxColumn2 = new DataGridViewTextBoxColumn();
			dataGridViewTextBoxColumn2.AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
			dataGridViewTextBoxColumn2.SortMode = DataGridViewColumnSortMode.NotSortable;
			dataGridViewTextBoxColumn2.Width = 80;
			dataGridViewTextBoxColumn2.Name = "房间编号";
			dataGridViewTextBoxColumn2.HeaderText = "房间编号";
			dataGridView.Columns.Add(dataGridViewTextBoxColumn2);
			DataGridViewTextBoxColumn dataGridViewTextBoxColumn3 = new DataGridViewTextBoxColumn();
			dataGridViewTextBoxColumn3.AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
			dataGridViewTextBoxColumn3.SortMode = DataGridViewColumnSortMode.NotSortable;
			dataGridViewTextBoxColumn3.Width = 80;
			dataGridViewTextBoxColumn3.Name = "房间名称";
			dataGridViewTextBoxColumn3.HeaderText = "房间名称";
			dataGridView.Columns.Add(dataGridViewTextBoxColumn3);
			DataGridViewTextBoxColumn dataGridViewTextBoxColumn4 = new DataGridViewTextBoxColumn();
			dataGridViewTextBoxColumn4.AutoSizeMode = DataGridViewAutoSizeColumnMode.NotSet;
			dataGridViewTextBoxColumn4.SortMode = DataGridViewColumnSortMode.NotSortable;
			dataGridViewTextBoxColumn4.Width = 105;
			dataGridViewTextBoxColumn4.Name = "标准照度";
			dataGridViewTextBoxColumn4.HeaderText = "标准照度(lx)";
			dataGridView.Columns.Add(dataGridViewTextBoxColumn4);
			DataGridViewTextBoxColumn dataGridViewTextBoxColumn5 = new DataGridViewTextBoxColumn();
			dataGridViewTextBoxColumn5.AutoSizeMode = DataGridViewAutoSizeColumnMode.NotSet;
			dataGridViewTextBoxColumn5.SortMode = DataGridViewColumnSortMode.NotSortable;
			dataGridViewTextBoxColumn5.Width = 105;
			dataGridViewTextBoxColumn5.Name = "实际照度";
			dataGridViewTextBoxColumn5.HeaderText = "实际照度(lx)";
			dataGridView.Columns.Add(dataGridViewTextBoxColumn5);
			DataGridViewTextBoxColumn dataGridViewTextBoxColumn6 = new DataGridViewTextBoxColumn();
			dataGridViewTextBoxColumn6.AutoSizeMode = DataGridViewAutoSizeColumnMode.NotSet;
			dataGridViewTextBoxColumn6.SortMode = DataGridViewColumnSortMode.NotSortable;
			dataGridViewTextBoxColumn6.Width = 105;
			dataGridViewTextBoxColumn6.Name = "标准功率密度";
			dataGridViewTextBoxColumn6.HeaderText = "标准功率密度(W/m2)";
			dataGridView.Columns.Add(dataGridViewTextBoxColumn6);
			DataGridViewTextBoxColumn dataGridViewTextBoxColumn7 = new DataGridViewTextBoxColumn();
			dataGridViewTextBoxColumn7.AutoSizeMode = DataGridViewAutoSizeColumnMode.NotSet;
			dataGridViewTextBoxColumn7.SortMode = DataGridViewColumnSortMode.NotSortable;
			dataGridViewTextBoxColumn7.Width = 105;
			dataGridViewTextBoxColumn7.Name = "实际功率密度";
			dataGridViewTextBoxColumn7.HeaderText = "实际功率密度(W/m2)";
			dataGridView.Columns.Add(dataGridViewTextBoxColumn7);
			DataGridViewTextBoxColumn dataGridViewTextBoxColumn8 = new DataGridViewTextBoxColumn();
			dataGridViewTextBoxColumn8.AutoSizeMode = DataGridViewAutoSizeColumnMode.NotSet;
			dataGridViewTextBoxColumn8.SortMode = DataGridViewColumnSortMode.NotSortable;
			dataGridViewTextBoxColumn8.Width = 105;
			dataGridViewTextBoxColumn8.Name = "距高比";
			dataGridViewTextBoxColumn8.HeaderText = "距高比";
			dataGridView.Columns.Add(dataGridViewTextBoxColumn8);
			dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
			return dataGridView;
		}

		private static void dgv_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
		{
			DataGridView dataGridView = (DataGridView)sender;
			if (e.ColumnIndex == 0 && dataGridView.Rows[e.RowIndex].Cells[0].Tag.ToString() != "")
			{
				MessageBox.Show(dataGridView.Rows[e.RowIndex].Cells[0].Tag.ToString());
			}
		}

		private void tsbRange_Click(object sender, EventArgs e)
		{
			if (this.isDisplay)
			{
				base.Size = new Size(779, 58);
				CaculateCount.i++;
				this.isDisplay = false;
			}
			this.MakeRequest(RequestId.Range);
		}

		private void tsbCurrentView_Click(object sender, EventArgs e)
		{
			this.MakeRequest(RequestId.CurrentView);
		}

		private void tsbCurrentProject_Click(object sender, EventArgs e)
		{
			this.MakeRequest(RequestId.CurrentProject);
		}

		public static void Range(UIApplication uiapp)
		{
			CaculateCount.roomNumList.Clear();
			CaculateCount.liCalculate.Clear();
			CaculateCount.j = 1;
			CaculateCount.tabLevels.TabPages.Clear();
			CmdCaculateCount cmdCaculateCount = new CmdCaculateCount(uiapp);
			List<AnnotationSymbol> li = cmdCaculateCount.FilterRectangle();
			IllCalDataSet calResultData = cmdCaculateCount.GetCalResultData(li);
			if (calResultData == null)
			{
				return;
			}
			CaculateCount.AddRoomData(calResultData, false);
			if (!CaculateCount.tabLevels.TabPages.ContainsKey(CaculateCount.activeView.GenLevel.Name))
			{
				TabPage tabPage = new TabPage();
				tabPage.Name = CaculateCount.activeView.GenLevel.Name;
				tabPage.Text = CaculateCount.activeView.GenLevel.Name;
				DataGridView dataGridView = CaculateCount.CreatDataGridView();
				tabPage.Controls.Add(dataGridView);
				dataGridView.Dock = DockStyle.Fill;
				CaculateCount.tabLevels.TabPages.Add(tabPage);
				CaculateCount.AddGridDataSource(dataGridView);
			}
		}

		public static void CurrentsProject(UIApplication uiapp)
		{
			CaculateCount.roomNumList.Clear();
			CaculateCount.liCalculate.Clear();
			CaculateCount.j = 1;
			CaculateCount.tabLevels.TabPages.Clear();
			CmdCaculateCount cmdCaculateCount = new CmdCaculateCount(uiapp);
			foreach (KeyValuePair<string, ElementId> keyValuePair in cmdCaculateCount.GetSystemFamilyElementList<Level>(uiapp.ActiveUIDocument.Document))
			{
				List<AnnotationSymbol> list = cmdCaculateCount.FileterProject(keyValuePair.Value);
				if (list.Count > 0)
				{
					IllCalDataSet calResultData = cmdCaculateCount.GetCalResultData(list);
					if (calResultData == null)
					{
						break;
					}
					CaculateCount.j = 1;
					CaculateCount.AddRoomData(calResultData, true);
				}
				DataGridView dataGridView = null;
				if (!CaculateCount.tabLevels.TabPages.ContainsKey(keyValuePair.Key))
				{
					TabPage tabPage = new TabPage();
					tabPage.Name = keyValuePair.Key;
					tabPage.Text = keyValuePair.Key;
					dataGridView = CaculateCount.CreatDataGridView();
					tabPage.Controls.Add(dataGridView);
					dataGridView.Dock = DockStyle.Fill;
					CaculateCount.tabLevels.TabPages.Add(tabPage);
				}
				if (dataGridView != null)
				{
					CaculateCount.AddGridDataSource(dataGridView);
				}
			}
		}

		public static void CurrentViews(UIApplication uiapp)
		{
			CaculateCount.roomNumList.Clear();
			CaculateCount.liCalculate.Clear();
			CaculateCount.j = 1;
			CaculateCount.tabLevels.TabPages.Clear();
			CmdCaculateCount cmdCaculateCount = new CmdCaculateCount(uiapp);
			List<AnnotationSymbol> li = cmdCaculateCount.FilterCurrentView();
			IllCalDataSet calResultData = cmdCaculateCount.GetCalResultData(li);
			if (calResultData != null)
			{
				CaculateCount.AddRoomData(calResultData, true);
			}
			if (CaculateCount.activeView.GenLevel != null && !CaculateCount.tabLevels.TabPages.ContainsKey(CaculateCount.activeView.GenLevel.Name))
			{
				TabPage tabPage = new TabPage();
				tabPage.Name = CaculateCount.activeView.GenLevel.Name;
				tabPage.Text = CaculateCount.activeView.GenLevel.Name;
				DataGridView dataGridView = CaculateCount.CreatDataGridView();
				tabPage.Controls.Add(dataGridView);
				dataGridView.Dock = DockStyle.Fill;
				CaculateCount.tabLevels.TabPages.Add(tabPage);
				CaculateCount.AddGridDataSource(dataGridView);
			}
		}

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void CaculateCount_MouseLeave(object sender, EventArgs e)
		{
			if (!base.ClientRectangle.Contains(base.PointToClient(Cursor.Position)))
			{
				int wMsg = 7;
				CaculateCount.SendMessage(WindowHelper.HWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
				return;
			}
			base.Focus();
		}

		private void CaculateCount_Leave(object sender, EventArgs e)
		{
			if (!base.ClientRectangle.Contains(base.PointToClient(Cursor.Position)))
			{
				int wMsg = 7;
				CaculateCount.SendMessage(WindowHelper.HWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
				return;
			}
			base.Focus();
		}

		protected override void OnFormClosed(FormClosedEventArgs e)
		{
			this.m_ExEvent.Dispose();
			this.m_ExEvent = null;
			this.m_Handler = null;
			base.OnFormClosed(e);
		}

		public void WakeUp()
		{
			if (!this.isDisplay)
			{
				base.Size = new Size(779, 459);
				CaculateCount.i++;
				this.isDisplay = true;
			}
		}

		private void MakeRequest(RequestId request)
		{
			this.m_Handler.Request.Make(request);
			this.m_ExEvent.Raise();
			this.DozeOff();
		}

		private void DozeOff()
		{
		}

		public static int i = 0;

		private Document doc;

		private UIDocument UIrvt_doc;

		private UIApplication ui_app;

		private bool isDisplay = true;

		public static List<CmdCaculaterDate> liCalculate = new List<CmdCaculaterDate>();

		public static List<string> roomNumList = new List<string>();

		public static int j = 1;

        public static Autodesk.Revit.DB.View activeView = null;

		private RequestHandler m_Handler;

		private ExternalEvent m_ExEvent;
	}
}
